package com.jp.knowledge.view;

import android.view.View;
import android.view.ViewTreeObserver;

import com.jp.knowledge.util.L;

import org.xutils.common.util.DensityUtil;

/**
 * Created by jiayen on 16/10/25.
 *
 * 监听软键盘的谈起与隐藏
 */

public class KeyBordLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
    private View rootView;

    private KeyBordListener  listener;


    public void setListener(KeyBordListener listener) {
        this.listener = listener;
    }

    public void setRootView(View rootView) {
        this.rootView = rootView;
    }

    private boolean  isShowkeyBord;
    @Override
    public void onGlobalLayout() {
        final int[] location = new int[2];
        rootView.getLocationOnScreen(location);

        int y = location[1];
        int talkViewHeight = rootView.getHeight();
        // 屏幕高度。这个高度不含虚拟按键的高度
        int screenHeight = DensityUtil.getScreenHeight();
        int dif=screenHeight-talkViewHeight;
        if(y>=(dif-10)){//软键盘隐藏
            if(isShowkeyBord){
                isShowkeyBord=false;
                if(listener!=null)
                    listener.onHideKeyboard();
            }
        }else {
            if(!isShowkeyBord){
                isShowkeyBord=true;
                if(listener!=null)
                    listener.onShowKeyboard();
            }
        }
        L.e("talkBottomview的坐标 x="+location[0]+";------y="+location[1]+"talkViewHeight高:"+talkViewHeight+";屏幕的高度为:"+screenHeight+";可用高度："+dif);
    }

    /**
     * 软键盘监听回调
     */
    public interface KeyBordListener{
        public void onHideKeyboard();
        public void onShowKeyboard();
    }
}
